<template>
  <UploadExcel :on-success="onSuccess" />
</template>

<script>
import { importEmployee } from '@/api/employees'
import {} from 'file-saver'
export default {
  methods: {
    async onSuccess(data) {
      console.log('这是data', data)
      const dict = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      const newData = data.results.map(user => {
        const newUser = {}
        for (const key in user) {
          const newKey = dict[key]
          let value = user[key]
          if (newKey === 'timeOfEntry' || newKey === 'correctionTime') {
            const timeStr = this.formatExcelTime(value)
            value = new Date(timeStr)
          }
          newUser[newKey] = value
        }
        return newUser
      })
      console.log('这是提交的最终数据', newData)
      await importEmployee(newData)
      this.$message.success('导入成功')
      this.$router.back()
    },
    formatExcelTime(num, format = '-') {
      num = Number(num)
      let millisecond = 0
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      } else {
        millisecond = (num - 25568) * 60 * 60 * 24 * 1000
      }
      const date = new Date(millisecond)
      const yy = date.getFullYear()
      const mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return yy + format + mm + format + dd
    }

  }

}
</script>

<style>

</style>
